package cn.xpg.tools;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendSms {
	// 发送短信
	public static void sendSms(String smsMob, String smsText) throws Exception {
		// int random = (int) (Math.random() * (999999 - 100000 + 1) + 100000);
		String uid = "tanxiaohua";// 平台账号
		String key = "5d8ed69c14187ed772a7";// 秘钥
		// 需发送的号码 smsMob
		// String smsText = "登录随机码：" + random;
		HttpClient client = new HttpClient();// 得到客户端
		PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn/");
		post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("Uid", uid), new NameValuePair("Key", key),
				new NameValuePair("smsMob", smsMob), new NameValuePair("smsText", smsText) };
		post.setRequestBody(data);
		client.executeMethod(post);

		String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
		System.out.println(result); // 打印返回消息状态
		post.releaseConnection();
	}

	// 发送验证码
	public static void sendYZ(String smsMob) throws Exception {
		int random = (int) (Math.random() * (999999 - 100000 + 1) + 100000);
		String uid = "tanxiaohua";// 平台账号
		String key = "5d8ed69c14187ed772a7";// 秘钥
		// 需发送的号码 smsMob
		String smsText = "登录随机码：" + random;
		HttpClient client = new HttpClient();// 得到客户端
		PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn/");
		post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
		NameValuePair[] data = { new NameValuePair("Uid", uid), new NameValuePair("Key", key),
				new NameValuePair("smsMob", smsMob), new NameValuePair("smsText", smsText) };
		post.setRequestBody(data);
		client.executeMethod(post);

		String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
		System.out.println(result); // 打印返回消息状态
		post.releaseConnection();
	}
}
